package com.mars.foodserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mars.foodserver.entity.Feedback;
import com.mars.foodserver.entity.FoodCategory;
import com.mars.foodserver.entity.User;
import com.mars.foodserver.exception.WorkException;
import com.mars.foodserver.mapper.FeedbackMapper;
import com.mars.foodserver.mapper.FoodCategoryMapper;
import com.mars.foodserver.mapper.UserMapper;
import com.mars.foodserver.response.base.BasePageRequest;
import com.mars.foodserver.response.base.PageInfo;
import com.mars.foodserver.service.IFeedbackService;
import com.mars.foodserver.service.IFoodCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wq
 * @since 2021-08-07
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void feedback(Integer userId, String content, String image) {
        if (StringUtils.isBlank(content)) {
            throw new WorkException("请输入反馈内容");
        }
        Feedback feedback = new Feedback();
        feedback.setFeedback(content);
        feedback.setImage(image);
        User user = userMapper.selectById(userId);
        if (Objects.nonNull(user)) {
            feedback.setUsername(user.getNickname());
        }
        feedback.setUserId(userId);
        save(feedback);
    }

    @Override
    public PageInfo<Feedback> pageList(BasePageRequest basePageRequest) {
        IPage<Feedback> page = new Page<>(basePageRequest.getPageNumber(), basePageRequest.getPageSize());
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Feedback::getCreateTime);
        List<Feedback> records = baseMapper.selectPage(page, wrapper).getRecords();
        return new PageInfo<>(page.getTotal(), records);
    }
}
